package com.prs.crm.action.sys;

import java.io.File;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import sun.security.krb5.internal.PAEncTSEnc;

import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.person.Department;
import com.prs.crm.domain.sys.AccountSet;
import com.prs.crm.domain.sys.PermissionGroup;
import com.prs.crm.domain.sys.SysDepartment;
import com.prs.crm.service.sys.AccountSetService;
import com.prs.crm.service.sys.PermissionGroupService;
import com.prs.crm.service.sys.SysDepartmentService;

public class AccountSetAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private AccountSet accountSet;
	private AccountSetService accountSetService;
	private List<SysDepartment> sysDepartment;
	private SysDepartmentService sysDepartmentService;
	private List<PermissionGroup> permissionGroup;
	private PermissionGroupService permissionGroupService;
	private File upload;
	private String uploadFileName;
	private File upload1;
	private String upload1FileName;
	private File upload3;
	private String upload3fileName;
	private List<AccountSet> account;
	private TreeNode root;

	public TreeNode getRoot() {
		return root;
	}
	public void setRoot(TreeNode root) {
		this.root = root;
	}
	public List<AccountSet> getAccount() {
		return account;
	}
	public void setAccount(List<AccountSet> account) {
		this.account = account;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUpload1() {
		return upload1;
	}
	public void setUpload1(File upload1) {
		this.upload1 = upload1;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUpload1FileName() {
		return upload1FileName;
	}
	public void setUpload1FileName(String upload1FileName) {
		this.upload1FileName = upload1FileName;
	}
	public String getUpload3fileName() {
		return upload3fileName;
	}
	public void setUpload3fileName(String upload3fileName) {
		this.upload3fileName = upload3fileName;
	}
	public File getUpload3() {
		return upload3;
	}
	public void setUpload3(File upload3) {
		this.upload3 = upload3;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public AccountSet getAccountSet() {
		return accountSet;
	}
	public void setAccountSet(AccountSet accountSet) {
		this.accountSet = accountSet;
	}
	public AccountSetService getAccountSetService() {
		return accountSetService;
	}
	public void setAccountSetService(AccountSetService accountSetService) {
		this.accountSetService = accountSetService;
	}

	public List<SysDepartment> getSysDepartment() {
		return sysDepartment;
	}
	public void setSysDepartment(List<SysDepartment> sysDepartment) {
		this.sysDepartment = sysDepartment;
	}
	public SysDepartmentService getSysDepartmentService() {
		return sysDepartmentService;
	}
	public void setSysDepartmentService(SysDepartmentService sysDepartmentService) {
		this.sysDepartmentService = sysDepartmentService;
	}
	public List<PermissionGroup> getPermissionGroup() {
		return permissionGroup;
	}
	public void setPermissionGroup(List<PermissionGroup> permissionGroup) {
		this.permissionGroup = permissionGroup;
	}
	public PermissionGroupService getPermissionGroupService() {
		return permissionGroupService;
	}
	public void setPermissionGroupService(
			PermissionGroupService permissionGroupService) {
		this.permissionGroupService = permissionGroupService;
	}
	
	@Action("listAccountSet")
	public String listAccountSet(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(AccountSet.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getAccountSetService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editAccountSet")
	public String editAccountSet(){
		if (ids!=null) {
			this.accountSet=this.getAccountSetService().get(ids[0]);
		}
		this.sysDepartment=this.getSysDepartmentService().getAllSysDepartment();
		this.permissionGroup=this.getPermissionGroupService().getAllPermissionGroup();
		return SUCCESS;
	}
	
	@Action("editPersonalInfo")
	public String editPersonalInfo(){
		return SUCCESS;
	}
	@Action("viewPersonalInfo")
	public String viewPersonalInfo(){
		return SUCCESS;
	}
	@Action(value="savePersonalInfo",results={
			@Result(name="success",type="redirect",location="editPersonalInfo?accountSet=${accountSet.getId()}"),
			@Result(name="error",type="redirect",location="editAccountSet")
	})
	public String savePersonalInfo(){
		if(accountSet.getUserId()==null||"".equals(accountSet.getUserId().trim())){
			addActionError("请输入userId!");
			return ERROR;
		}
		if(accountSet.getName()==null||"".equals(accountSet.getName().trim())){
			addActionError("请输入姓名!");
			return ERROR;
		}
		accountSet.setType(0);
		accountSet.setPassword("123456");
		this.getAccountSetService().save(accountSet);
		return SUCCESS;
	}
	@Action(value="savePersonalInfos",results={
			@Result(name="success",type="redirect",location="editAccountSet?accountSet=${accountSet.getId()}"),
			@Result(name="error",type="redirect",location="editPersonalInfo?accountSet=${accountSet.getId()}")
	})
	public String savePersonalInfos(){
		if(upload3!=null){
			this.accountSet.setAttach3(FileUtil.getInstance().createAttach(upload3, upload3fileName));
		}
		this.getAccountSetService().save(accountSet);
		return SUCCESS;
	}
	@Action(value="saveAccountSet",results={
			@Result(name="success",type="redirect",location="listAccountSet"),
			@Result(name="error",type="redirect",location="editAccountSet")
	})
	public String saveAccountSet(){
		if(upload!=null){
			this.accountSet.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		if(upload1!=null){
			this.accountSet.setAttach1(FileUtil.getInstance().createAttach(upload1, upload1FileName));
		}
		accountSet.setType(0);
		accountSet.setPassword("123456");
		this.getAccountSetService().save(accountSet);
		return SUCCESS;
	}
	@Action(value="savePass",results={
			@Result(name="success",type="redirect",location="listAccountSet"),
			@Result(name="error",type="redirect",location="listAccountSet")
	})
	public String savePass(){
		if(ids!=null){
			this.accountSet=this.getAccountSetService().get(ids[0]);
		}
			accountSet.setPassword("123456");
			this.getAccountSetService().submit(accountSet);
			return SUCCESS;
	}
	@Action(value="saveType",results={
			@Result(name="success",type="redirect",location="listAccountSet"),
			@Result(name="error",type="redirect",location="listAccountSet")
	})
	public String saveType(){
		if(ids!=null){
			this.accountSet=this.getAccountSetService().get(ids[0]);
		}
		accountSet.setType(1);
		this.getAccountSetService().save(accountSet);
		return SUCCESS;
	}
	@Action(value="saveType1",results={
			@Result(name="success",type="redirect",location="listAccountSet"),
			@Result(name="error",type="redirect",location="listAccountSet")
	})
	public String saveType1(){
		if(ids!=null){
			this.accountSet=this.getAccountSetService().get(ids[0]);
		}
		accountSet.setType(0);
		this.getAccountSetService().save(accountSet);
		return SUCCESS;
	}
	
	@Action(value="saveAccountSets",results={
			@Result(name="success",type="redirect",location="editAccountSet"),
			@Result(name="error",type="redirect",location="editAccountSet")
	})
	public String saveAccountSets(){
		if(upload!=null){
			this.accountSet.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		if(upload1!=null){
			this.accountSet.setAttach1(FileUtil.getInstance().createAttach(upload1, upload1FileName));
		}
		accountSet.setType(0);
		accountSet.setPassword("123456");
		this.getAccountSetService().save(accountSet);
		return SUCCESS;
	}
	@Action(value="removeAccountSet",results={
			@Result(name="success",type="redirect",location="listAccountSet"),
			@Result(name="error",type="redirect",location="listAccountSet")
	})
	public String removeAccountSet(){
		if (ids!=null) {
			this.accountSet=this.getAccountSetService().get(ids[0]);
		}
		this.getAccountSetService().batchRemove(ids);
		return SUCCESS;
	}
	@Action(value = "loadSysDepartmentTree", results = { @Result(name = "success", type = "json",
			params = {"root","root"}) })
	public String loadTree(){
		root=new TreeNode();
		root.setId("-1");
		TreeNode node=new TreeNode();
		node.setId("0");
		node.setText("人员选择");
		
		Collection<SysDepartment> depart=this.getSysDepartmentService().getAllRootSysDepartment();
		for (SysDepartment sysDepartments : depart) {
			TreeNode nodes=new TreeNode();
			nodes.setText(sysDepartments.getName());
			nodes.setId("sysDepartment-"+sysDepartments.getId().toString());
			node.addChildNode(nodes);
			addSysDepartmentNode(nodes, sysDepartments);
			addAccountSetNode(nodes, sysDepartments);
		}
		root.addChildNode(node);
		return SUCCESS;
	}
	public void addAccountSetNode(TreeNode parent,SysDepartment sysDepartment){
		for (AccountSet accountSet : this.getAccountSetService().findBySysDepartment(sysDepartment)) {
			TreeNode node=new TreeNode();
			if(accountSet!=null){
				node.setText(accountSet.getName());
			}
			node.setId(accountSet.getId().toString());
			parent.addChildNode(node);
		}
	}
	public void addSysDepartmentNode(TreeNode parent,SysDepartment sysDepartment){
		for (SysDepartment depart : sysDepartment.getChilds()) {
			TreeNode node=new TreeNode();
			node.setText(depart.getName());
			node.setId("sysDepartment-"+depart.getId().toString());
			parent.addChildNode(node);
			addSysDepartmentNode(node, depart);
			addAccountSetNode(node, depart);
		}
	}
	
	@Action("listWorkManagerPermission")
	public String listWorkManagerPermission(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(AccountSet.class);
		criteria.add(Restrictions.eq("type", 0));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getAccountSetService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	

	@Action("editWorkManagerPermission")
	public String editWorkManagerPermission(){
		if (ids!=null) {
			this.accountSet=this.getAccountSetService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action(value="saveWorkManagerPermission",results={
			@Result(name="success",type="redirect",location="listWorkManagerPermission"),
			@Result(name="error",type="chain",location="editWorkManagerPermission")
	})
	public String saveWorkManagerPermission(){
		this.getAccountSetService().save(accountSet);
		return SUCCESS;
	}
}
